package com.topboya.linker.ui.activity;


import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.master.permissionhelper.PermissionHelper;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.GuideLoginPrestener;
import com.topboya.linker.ui.view.GuideLoginView;
import com.topboya.linker.utils.CacheUtils;
import com.topboya.linker.utils.LogUtils;


public class SplashActivity extends BaseActivity<GuideLoginView,GuideLoginPrestener> implements GuideLoginView {
    private static final String TAG = "SplashActivity";
    private ImageView img_splash;
    private PermissionHelper permissionHelper;
    @Override
    protected int getLayoutId() {

        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {

        img_splash =findViewById(R.id.img_splash);
        img_splash.setBackground(getResources().getDrawable(R.drawable.splash));
        setAnimation();
    }


    private void setAnimation() {

        //渐变
        AlphaAnimation aa = new AlphaAnimation(0, 1);
        aa.setFillAfter(true);

        //缩放
        ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        sa.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);

        //添加动画，1500ms
        set.addAnimation(aa);
        set.addAnimation(sa);
        set.setDuration(1000);

        img_splash.startAnimation(set);

        set.setAnimationListener(new GuideAnimationListener());
    }

    private class GuideAnimationListener implements Animation.AnimationListener {
        //动画开始播放时回调
        @Override
        public void onAnimationStart(Animation animation) {

        }

        //动画播放结束时回调
        @Override
        public void onAnimationEnd(Animation animation) {

            permissionCheck();

        }

        //动画重复播放时回调
        @Override
        public void onAnimationRepeat(Animation animation) {


        }
    }

    private void permissionCheck() {

        permissionHelper = new PermissionHelper(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA,Manifest.permission.READ_PHONE_STATE,Manifest.permission.ACCESS_FINE_LOCATION}, 100);
        permissionHelper.request(new PermissionHelper.PermissionCallback() {
            @Override
            public void onPermissionGranted() {
                //全都授权
                LogUtils.v(TAG+"onPermissionGranted...");
                gotoActivity();
            }

            @Override
            public void onIndividualPermissionGranted(String[] grantedPermission) {

                //某个授权
                LogUtils.v(TAG+"onIndividualPermissionGranted() called with: grantedPermission = [" + TextUtils.join(",",grantedPermission) + "]");
            }

            @Override
            public void onPermissionDenied() {
                //某个拒绝
                LogUtils.v(TAG+"onPermissionDenied...");
                finish();
            }

            @Override
            public void onPermissionDeniedBySystem() {
                //用户选择了"不再询问"后，点击"拒绝按钮"，执行此方法
                LogUtils.v(TAG+"onPermissionDeniedBySystem...");
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (permissionHelper != null) {
            permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
    @Override
    protected void initData() {


    }

    @Override
    protected void initListener() {

    }

    @Override
    protected GuideLoginPrestener createPresenter() {

        return new GuideLoginPrestener(this);
    }


    @Override
    public void getLoginResult() {

    }


    public void gotoActivity(){

        String phone = CacheUtils.getLoginname(this,AppConst.PHONE);
        String pw  = CacheUtils.getLogipw(this,AppConst.PW);
        LogUtils.v(TAG + "phone:"+phone +"pw:"+phone);
        boolean isMain = CacheUtils.getBoolean(this,AppConst.ISMAIN);
        Intent intent = null;
//       表示没有进入过guide页面，进入guide页面
        if (isMain){

            intent = new Intent(this,GuideAcitvity.class);
            startActivity(intent);
            finish();
        }else {

            if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pw)){

                intent = new Intent(this,LoginActivity.class);
                startActivity(intent);
                finish();

            }else {
//                直接登录
                mPresenter.login(phone,pw);
            }
        }


    }
}
